package com.ia2014.tp.portal2.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ia2014.tp.portal2.vo.ArticuloVO;
import com.ia2014.tp.portal2.vo.ItemVentaVO;
import com.ia2014.tp.portal2.vo.UsuarioVO;
import com.ia2014.tp.portal2.vo.VentaVO;

/**
 * Se define Servlet abstracto para manejar sesiones y otras operaciones
 * comunes de servlets
 * @author Diego Alejandro
 *
 */
public abstract class AbstractServlet extends HttpServlet{
	private static final long serialVersionUID = 3582246659193622154L;
	protected Map<Integer, ItemVentaVO>itemsVo;
	
	protected void comprobarSession(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException{
		
		if (request.getSession(false) == null){
			response.sendRedirect("login.jsp?error=1");
		}
	}
	
	protected void setListArticulosToSession(HttpSession session, List<ArticuloVO> articulosVO)throws ServletException, IOException{
		session.setAttribute("articulosList", articulosVO);
	}
	
	protected List<ArticuloVO> getListFromSession(HttpSession session) throws ServletException, IOException{
		List<ArticuloVO> articulos = (List<ArticuloVO>) session.getAttribute("articulosList");
		if (articulos == null){
			articulos = new ArrayList<ArticuloVO>();
		}
		
		return articulos;
	}
	
	protected Map<Integer, ItemVentaVO> getMapItemsFromSession(HttpSession session) throws ServletException, IOException{
		itemsVo = (Map<Integer, ItemVentaVO>) session.getAttribute("itemsMap");
		if (itemsVo == null){
			itemsVo = new Hashtable<Integer, ItemVentaVO>();
			session.setAttribute("itemsMap", itemsVo);
		}
		
		return itemsVo;
	}
	
	protected Map<Integer, ItemVentaVO> vaciarMapItemFromSession(HttpSession session) throws ServletException, IOException{
		itemsVo = new Hashtable<Integer, ItemVentaVO>();
		session.setAttribute("itemsMap", itemsVo);
		return itemsVo;
	}
	
	protected UsuarioVO getUsuarioVOFromSession(HttpSession session)throws ServletException, IOException{
		UsuarioVO vo = (UsuarioVO) session.getAttribute("usuarioVO");
		return vo;
	}
	
	protected void setUsuarioVOToSession(HttpSession session, UsuarioVO usuarioVO)throws ServletException, IOException{
		session.removeAttribute("usuarioVO");
		session.setAttribute("usuarioVO", usuarioVO);
	}	
	
	protected void setVentasVOToSession(HttpSession session, List<VentaVO> ventasVO)throws ServletException, IOException{
		Double total = 0.0;
		Integer cantidad = 0;
		Map<Integer, VentaVO> ventasMap = new Hashtable<Integer, VentaVO>();
		
		if (ventasVO != null){
			session.setAttribute("listVentas", ventasVO);
			cantidad = ventasVO.size();
			
			for (VentaVO venta : ventasVO){
				total += venta.getMontoTotal();
				ventasMap.put(venta.getNroVenta(), venta);
			}
			
		}
		else{
			session.setAttribute("listVentas", new ArrayList<VentaVO>());
		}
		session.setAttribute("mapVentas", ventasMap);
		session.setAttribute("cantListVentas", cantidad);
		session.setAttribute("totalListVentas", total);
	}
	
	public Map<Integer, VentaVO> getMapVentasFromSession(HttpSession session)throws ServletException, IOException{
		Map<Integer, VentaVO> ventasMap = (Map<Integer, VentaVO>) session.getAttribute("mapVentas");
		if (ventasMap == null){
			ventasMap = new Hashtable<Integer, VentaVO>();
		}
		return ventasMap;
	}
}
